#include "adduser.h"
#include "ui_adduser.h"

AddUser::AddUser(QString roleId,QWidget *parent) :
    QDialog(parent),
    ui(new Ui::AddUser)
{
    ui->setupUi(this);
    setWindowIcon(QIcon(":/image/res/stm.jpg"));
    QPixmap pix(":/image/res/search.jpg");
    pix.scaled(25,25);
    ui->btn_search->setIcon(pix);
    QPixmap freshPix(":/image/res/refresh.jpg");
    ui->btn_reset->setIcon(freshPix.scaled(25,25));
    ui->lineEdit_gotoPage->setValidator(new QRegularExpressionValidator(QRegularExpression("[0-9]*")));
    setWindowTitle("添加用户");

    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);

    this->roleId = roleId;
    pageNum = 1;
    pageSize = 10;
    currentPage = 1;

    init();
}

AddUser::~AddUser()
{
    delete ui;
}

void AddUser::init()
{
    getList();
    initTable();
    initPageInfo();
}

void AddUser::getList()
{
    userList.clear();
    QByteArray *data = roleManagerApi.unallocatedList(pageNum,pageSize,roleId,
                ui->lineEdit_userName->text(),ui->lineEdit_phone->text());
    QJsonObject obj = QJsonDocument::fromJson(*data).object();
    QJsonArray arr = obj.value("rows").toArray();
    total = obj.value("total").toInt();
    for(int i = 0;i< arr.size();i++){
        QJsonObject userObj = arr.at(i).toObject();
        User user = User::toUser(userObj);
        userList.append(user);
    }
    if(total % pageSize == 0){
        totalPage = total / pageSize;
    }else{
        totalPage = total / pageSize + 1;
    }
    ui->label_count->setText("共"+QString::number(total)+"条");
}

void AddUser::initTable()
{
    ui->tableWidget->clear();
    QStringList columnHeader;
    columnHeader << "用户名称" << "用户昵称" << "邮箱" << "手机" << "状态" << "创建时间" << "id";
    ui->tableWidget->setColumnCount(columnHeader.size());
    ui->tableWidget->setHorizontalHeaderLabels(columnHeader);
    ui->tableWidget->setRowCount(userList.size());
    for(int i = 0;i< userList.size();i++){
        QString userId = QString::number(userList.at(i).userId);
        ui->tableWidget->setItem(i,0,new QTableWidgetItem(userList.at(i).userName));
        ui->tableWidget->setItem(i,1,new QTableWidgetItem(userList.at(i).nickName));
        ui->tableWidget->setItem(i,2,new QTableWidgetItem(userList.at(i).email));
        ui->tableWidget->setItem(i,3,new QTableWidgetItem(userList.at(i).phonenumber));
        ui->tableWidget->setItem(i,6,new QTableWidgetItem(userId));
        ui->tableWidget->hideColumn(6);
        QLabel *label = new QLabel();
        label->setText(userList.at(i).status == "0" ? "正常" : "停用");
        label->setFixedSize(46,20);
        label->setAlignment(Qt::AlignCenter);
        label->setStyleSheet(userList.at(i).status == "0" ?
                                 "background-color:rgb(230, 242, 253);color:rgb(24, 144, 255);border:1px solid rgb(209,233,255);border-radius:3px;" :
                                 "background-color:rgb(255, 237, 237);color:rgb(255, 73, 73);border:1px solid rgb(255,219,219);border-radius:3px;");
        QWidget *widget = new QWidget();
        QHBoxLayout* hLayout = new QHBoxLayout(widget);
        // 设置QLabel的对齐方式为居中
        label->setAlignment(Qt::AlignCenter);
        // 将QLabel添加到QHBoxLayout和QVBoxLayout
        hLayout->addWidget(label);
        ui->tableWidget->setCellWidget(i,4,widget);
        ui->tableWidget->setItem(i,5,new QTableWidgetItem(userList.at(i).createTime));
    }
    // 设置单元格内容居中对齐
    for (int row = 0; row < ui->tableWidget->rowCount(); ++row) {
        for (int column = 0; column < ui->tableWidget->columnCount(); ++column) {
            QTableWidgetItem* item = ui->tableWidget->item(row, column);
            if (item) {
                item->setTextAlignment(Qt::AlignCenter);
            }
        }
    }
}

void AddUser::initPageInfo()
{
    if(currentPage == 1){
        ui->btn_pervious->setEnabled(0);
    }else{
        ui->btn_pervious->setEnabled(1);
    }
    ui->label_currentPage->setText(QString::number(currentPage));
    if(currentPage == totalPage){
        ui->btn_next->setEnabled(0);
    }else{
        ui->btn_next->setEnabled(1);
    }
}


void AddUser::on_btn_cancel_clicked()
{
    close();
}


void AddUser::on_btn_search_clicked()
{
    init();
}


void AddUser::on_btn_reset_clicked()
{
    ui->lineEdit_userName->clear();
    ui->lineEdit_phone->clear();
    init();
}


void AddUser::on_comboBox_pageItem_currentIndexChanged(int index)
{
    switch (index) {
    case 0:
        pageSize = 5;
        break;
    case 1:
        pageSize = 10;
        break;
    case 2:
        pageSize = 20;
        break;
    case 3:
        pageSize = 50;
        break;
    case 4:
        pageSize = 100;
        break;
    default:
        break;
    }
    currentPage = 1;
    getList();
    initTable();
    initPageInfo();
}


void AddUser::on_btn_pervious_clicked()
{
    ui->btn_pervious->setEnabled(0);
    currentPage--;
    pageNum--;
    init();
    ui->btn_pervious->setEnabled(1);
    initPageInfo();
}


void AddUser::on_btn_next_clicked()
{
    ui->btn_next->setEnabled(0);
    currentPage++;
    pageNum++;
    init();
    ui->btn_next->setEnabled(1);
    initPageInfo();
}

void AddUser::on_btn_ok_clicked()
{
    QList<QTableWidgetItem*> items = ui->tableWidget->selectedItems();
    if(items.isEmpty()) return;
    int rowCount = items.size() / 5;
    QString ids;
    for(int i = 0;i< rowCount;i++){
        int index = ui->tableWidget->row(items.at(i * 5));
        if(i != rowCount - 1){
            ids += ui->tableWidget->item(index,6)->text() + ",";
        }else{
            ids += ui->tableWidget->item(index,6)->text();
        }
    }
    QByteArray *bytes = roleManagerApi.selectAll(roleId,ids);
    QJsonObject obj = QJsonDocument::fromJson(*bytes).object();
    if(200 == obj.value("code").toInt()){
        QMessageBox::warning(0,"提示",obj.value("msg").toString());
        close();
        emit authSuccess();
    }
}


void AddUser::on_lineEdit_gotoPage_returnPressed()
{
    int page = ui->lineEdit_gotoPage->text().toInt();
    ui->lineEdit_gotoPage->clear();
    if(page < 1 || page > totalPage){
        ui->lineEdit_gotoPage->setStyleSheet("border:1px solid red;");
        return;
    }
    ui->lineEdit_gotoPage->setStyleSheet("");
    currentPage = page;
    getList();
    initTable();
    initPageInfo();
}

